﻿using CSharpToolWPF.Pages;
using CSharpToolWPF.Windows;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Windows;

namespace CSharpToolWPF {
    public class ClassAndLibData {
        public ClassAndLibData(string className, string libName, bool isInValidNow = false, bool isExternalLib = false) {
            ClassName = className;
            LibName = libName;
            IsInValidNow = isInValidNow;
            IsExternalLib = isExternalLib;
        }
        public string ClassName { get; set; }
        public string LibName { get; set; }
        public bool IsInValidNow { get; set; }
        public bool IsExternalLib { get; set; }
    }
    // 目前不可用(比如已被拆分)的库列表
    public class InValidLibData {
        public InValidLibData(string libName) {
            LibName = libName;
        }
        public string LibName { get; set; }
    }
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application {
        // 定义一个私有的IServiceProvider来保存服务提供者
        public IServiceProvider serviceProvider;

        // 类与库对应表
        public ObservableCollection<ClassAndLibData> ListClassAndLib { get; set; }
        private string listSavePath = "./Data/ClassAndLibList.json";
        
        public ObservableCollection<InValidLibData> ListInValidLib { get; set; }
        public string listInValidLibSavePath = "./Data/InValidLibList.json";
        // 
        public void SaveList() {
            // 保存 app.ListClassAndLib
            string json = JsonConvert.SerializeObject(ListClassAndLib, Newtonsoft.Json.Formatting.Indented);
            if (!Directory.Exists("./Data")) {
                Directory.CreateDirectory("./Data");
            }
            File.WriteAllText(this.listSavePath, json);
            // 保存 app.ListClassAndLib
            json = JsonConvert.SerializeObject(ListInValidLib, Newtonsoft.Json.Formatting.Indented);
            if (!Directory.Exists("./Data")) {
                Directory.CreateDirectory("./Data");
            }
            File.WriteAllText(this.listInValidLibSavePath, json);
        }

        public App() {
            this.DispatcherUnhandledException += Application_DispatcherUnhandledException;
            // 初始化服务容器
            ServiceCollection services = new ServiceCollection();

            // 注册服务到容器中
            ConfigureServices(services);

            // 构建服务提供者
            serviceProvider = services.BuildServiceProvider();

            // 使应用支持 GB2312 编码
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            // 初始化类与库对应表
            // ListClassAndLib 加载
            string jsonListClassAndLib = null;
            if (File.Exists(this.listSavePath)) {
                jsonListClassAndLib = File.ReadAllText(this.listSavePath);
            }
            if (jsonListClassAndLib != null) {
                ListClassAndLib = JsonConvert.DeserializeObject<ObservableCollection<ClassAndLibData>>(jsonListClassAndLib);
            }
            if (ListClassAndLib == null) {
                ListClassAndLib = new();
            }
            //datagridClassLib.ItemsSource = listClassAndLib;
            // ListInValidLib 加载
            string jsonInValidLib = null;
            if (File.Exists(this.listInValidLibSavePath)) {
                jsonInValidLib = File.ReadAllText(this.listInValidLibSavePath);
            }
            if (jsonInValidLib != null) {
                ListInValidLib = JsonConvert.DeserializeObject<ObservableCollection<InValidLibData>>(jsonInValidLib);
            }
            if (ListInValidLib == null) {
                ListInValidLib = new();
            }
        }
        
        // 配置服务的方法
        private void ConfigureServices(ServiceCollection services) {
            // 注册服务，比如你的ViewModel或其他依赖
            services.AddSingleton<PageAddProjectToNewSln>();
            services.AddSingleton<PageAutoAddDenpendencies>();
            services.AddSingleton<PageTest>(); 
            services.AddSingleton<PageCheckNotAddedProjects>(); 
            services.AddSingleton<WindowInteractivelyFillBlank>();
        }
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
            // 显示错误信息给用户
            MessageBox.Show("发生错误: " + e.Exception.InnerException.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);

            // 防止程序崩溃
            e.Handled = true;
        }
    }

}
